package com.jzo2o.foundations.service.impl;

import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description HomeServiceImpl
 * @Author luohai
 * @Date 2024-12-05
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private ServeMapper serveMapper;

    /**
     * 首页服务列表
     *
     * @param regionId
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,key = "#regionId",cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeCategoryResDTO> firstPageServeList(Long regionId) {
        List<ServeCategoryResDTO> list = serveMapper.firstPageServeList(regionId);
        int size = list.size() >= 2 ? 2 : list.size();
        list = list.stream().limit(size).collect(Collectors.toList());
        for (ServeCategoryResDTO serveCategoryResDTO : list) {
            int size1 = serveCategoryResDTO.getServeResDTOList().size() >= 4 ? 4 : serveCategoryResDTO.getServeResDTOList().size();
            List<ServeSimpleResDTO> collect = serveCategoryResDTO.getServeResDTOList().stream().limit(size1).collect(Collectors.toList());
            serveCategoryResDTO.setServeResDTOList(collect);
        }
        return list;
    }

    /**
     * 服务类型列表
     * @param regionId
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE,key = "#regionId",cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeAggregationTypeSimpleResDTO> serveTypeList(Long regionId) {
        List<ServeAggregationTypeSimpleResDTO> serveAggregationTypeSimpleResDTO=serveMapper.serveTypeList(regionId);
        return serveAggregationTypeSimpleResDTO;
    }

    /**
     * 首页热门服务列表
     * @param regionId
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.HOT_SERVE,key = "#regionId",cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {
        List<ServeAggregationSimpleResDTO> list=serveMapper.hotServeList(regionId);
        return list;
    }

    /**
     * 根据id查询服务
     * @param id
     * @return
     */
    @Override
    public ServeAggregationSimpleResDTO getServeById(Long id) {
        return serveMapper.getServeById(id);
    }


}
